package com.yyyyglxt.yygl.workdayManage.controller;

import com.yyyyglxt.common.controller.BaseController;
import com.yyyyglxt.common.utils.PageUtils;
import com.yyyyglxt.common.utils.Query;
import com.yyyyglxt.common.utils.R;
import com.yyyyglxt.system.service.UserService;
import com.yyyyglxt.yygl.recordManage.domain.RecordDO;
import com.yyyyglxt.yygl.recordManage.service.RecordService;
import com.yyyyglxt.yygl.workdayManage.domain.WorkdayDO;
import com.yyyyglxt.yygl.workdayManage.service.WorkdayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author qichao
 * @date 2022/4/27 17:25
 */

@Controller
@RequestMapping("/yygl/workdayPage")
public class WorkdayController extends BaseController {
    static final String prefix = "/yygl/workdayPage";

    @Autowired
    private WorkdayService workdayService;

    @Autowired
    private RecordService recordService;

    @GetMapping("/main")
    public String main(){
        return prefix+"/main";
    }

    //根据医生显示排班信息列表
    @GetMapping("/listByDoc")
    public String listByDoc(@RequestParam Map<String, Object> params,Model model){
        fixedListByDoc(params,model);
        liveListByDoc(params,model);
        return prefix+"/listByDoc";
    }

    //根据医生将固定排班信息列表封装到model
    public void fixedListByDoc(@RequestParam Map<String, Object> params,Model model){
        List<WorkdayDO> workdayList = workdayService.list(params);
//        model.addAttribute("workdayList",workdayList);
        //获取上午的排班信息，并且按照星期几进行排序
        List<WorkdayDO> amWorkdayList = workdayList.stream().filter(item ->  "上午".equals(item.getAmpm())).sorted(Comparator.comparing(WorkdayDO::getWorktime)).collect(Collectors.toList());
        //获取下午的排班信息，并且按照星期几进行排序
        List<WorkdayDO> pmWorkdayList = workdayList.stream().filter(item ->  "下午".equals(item.getAmpm())).sorted(Comparator.comparing(WorkdayDO::getWorktime)).collect(Collectors.toList());
        model.addAttribute("amWorkdayList",amWorkdayList);
        model.addAttribute("pmWorkdayList",pmWorkdayList);
    }

    //根据医生将实时排班信息列表封装到model
    public void liveListByDoc(@RequestParam Map<String, Object> params,Model model){
        /*** 获取医生的固定排班 ***/
        List<WorkdayDO> workdayList = workdayService.list(params);
        //获取接下来7天的日期
        String[] nextWeekDate = new String[7];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int weekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(weekday <= 0) weekday = 7;
        weekday-=1;//星期对应下标  比如周六 是 5
        model.addAttribute("weekday",weekday+1);
        for (int i=0;i<7;i++){
            nextWeekDate[weekday] = sdf.format(cal.getTime());
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
            weekday++;
            weekday%=7;
        }
        model.addAttribute("nextWeekDate",nextWeekDate);
        /*** 根据预约情况，计算实时号源信息 ***/
        List<RecordDO> recordList = recordService.list(params);
        //获取上午的实时排班信息
        List<WorkdayDO> amLiveWorkdayList = workdayList.stream().filter(item ->  "上午".equals(item.getAmpm())).sorted(Comparator.comparing(WorkdayDO::getWorktime)).collect(Collectors.toList());
        if(amLiveWorkdayList.size()==0) return;//管理员没有排班信息
        for (int i = 0; i < nextWeekDate.length; i++) {
            String dateStr = nextWeekDate[i];
            int amRecordCount = recordList.stream().filter(item -> sdf.format(item.getVisitdate()).equals(dateStr) && item.getVisitnoon().equals("上午")&&item.getState().equals("成功")).collect(Collectors.toList()).size();
            amLiveWorkdayList.get(i).setNsnum(amLiveWorkdayList.get(i).getNsnum()-amRecordCount);
        }
        model.addAttribute("amLiveWorkdayList",amLiveWorkdayList);
        //获取上午的实时排班信息
        List<WorkdayDO> pmLiveWorkdayList = workdayList.stream().filter(item ->  "下午".equals(item.getAmpm())).sorted(Comparator.comparing(WorkdayDO::getWorktime)).collect(Collectors.toList());
        for (int i = 0; i < nextWeekDate.length; i++) {
            String dateStr = nextWeekDate[i];
            int pmRecordCount = recordList.stream().filter(item -> sdf.format(item.getVisitdate()).equals(dateStr) && item.getVisitnoon().equals("下午")&&item.getState().equals("成功")).collect(Collectors.toList()).size();
            pmLiveWorkdayList.get(i).setNsnum(pmLiveWorkdayList.get(i).getNsnum()-pmRecordCount);
        }
        model.addAttribute("pmLiveWorkdayList",pmLiveWorkdayList);
    }

    //医生排班申请主页
    @GetMapping("/wrokdayApply")
    public String wrokdayApply(@RequestParam Map<String, Object> params,Model model){
        params.put("did",getUserId());
        fixedListByDoc(params,model);
        liveListByDoc(params,model);
        return prefix+"/wrokdayApply";
    }


//    @ResponseBody
//    @GetMapping("/list")
//    @RequiresPermissions("system:workday:workday")
//    public PageUtils list(@RequestParam Map<String, Object> params){
//        //查询列表数据
//        Query query = new Query(params);
//        List<WorkdayDO> workdayList = workdayService.list(query);
//        int total = workdayService.count(query);
//        PageUtils pageUtils = new PageUtils(workdayList, total);
//        return pageUtils;
//    }

    @GetMapping("/add")
    @RequiresPermissions("system:workday:add")
    String add(){
        return "system/workday/add";
    }

    @GetMapping("/edit/{wid}")
    String edit(@PathVariable("wid") Integer wid, Model model){
        WorkdayDO workday = workdayService.get(wid);
        model.addAttribute("workday", workday);
        return "system/workday/edit";
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update( WorkdayDO workday){
        workdayService.update(workday);
        return R.ok();
    }
}
